home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / PowerD / alpha / examples / Thread2.d < prev    next >
Encoding:
Text File  |  2002-10-28  |  1.7 KB  |  77 lines

  1. MODULE 'dos/dos','dos/dostags','dos/dosextens','utility/tagitem','exec/nodes'
  2. MODULE 'exec/ports','exec/memory'
  3.  
  4. PROC main()
  5.   -> message for interprocess communication
  6.   DEF message:PTR TO MN
  7.   -> port of main process
  8.   DEF port:PTR TO MP
  9.   -> pointer to the thread process
  10.   DEF mythread:PTR TO Process
  11.  
  12.   -> port to talk with thread
  13.   IF port:=CreateMsgPort()
  14.  
  15.     -> allocate message
  16.     IF message:=AllocVec(SIZEOF_MN,MEMF_CLEAR OR MEMF_PUBLIC)
  17.  
  18.       -> fill in message node
  19.       message::LN.Type:=NT_MESSAGE
  20.       message.Length:=SIZEOF_MN
  21.       message.ReplyPort:=port
  22.  
  23.       -> create a thread process
  24.       IF mythread:=CreateNewProc(
  25.         [
  26.         NP_Entry,&thread, -> where the thread process begins
  27.         NP_Name,'MyThread', -> the thread process name
  28.         TAG_DONE
  29.         ])
  30.  
  31.         -> send the thread a startup message
  32.         PutMsg(mythread.MsgPort,message)
  33.  
  34.         /* main program here */
  35.  
  36.        -> wait for the threads' death
  37.         WaitPort(port)
  38.  
  39.       ENDIF
  40.       FreeVec(message)
  41.     ENDIF
  42.     DeleteMsgPort(port)
  43.   ENDIF
  44. ENDPROC
  45.  
  46. PROC thread()
  47.   -> pointer to this process
  48.   DEF thisthread:PTR TO Process
  49.   -> pointer to the received message
  50.   DEF message:PTR TO MN
  51.  
  52.   -> find out about ourselves
  53.   thisthread:=FindTask(0)
  54.  
  55.   -> wait for the startup message (sent by the main process)
  56.   WaitPort(thisthread.MsgPort)
  57.  
  58.   -> get the startup message
  59.   message:=GetMsg(thisthread.MsgPort)
  60.  
  61.   /* thread program begins here */
  62.  
  63.   -> useless program, just waits a second
  64.   WriteF('Hello, I\am a newbie thread. It\as nice to be here.\n')
  65.   Delay(50)
  66.  
  67.   /* thread program ends here */
  68.  
  69.   -> make sure there is no taskswitching after we replied the message
  70.   Forbid()
  71.  
  72.   -> reply to main process
  73.   ReplyMsg(message)
  74.  
  75. -> thread dies here
  76. ENDPROC
  77.